package com.lyh.hkcloud.module.common;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: zhongfan
 * @program: hkCloud
 * @description:
 * @company—date: 利元数据 2022-02-14 14:51
 **/
public abstract class MyBaseAdapter<T> extends BaseAdapter {
    protected Context mcontext;
    protected LayoutInflater mlayoutInflater;
    protected List<T> mlist = new ArrayList<T>();


    public MyBaseAdapter(Context context) {
        mcontext = context;
        mlayoutInflater = LayoutInflater.from(context);
    }


    /**
     * 判断list是否为空
     */
    @Override
    public boolean isEmpty() {
        return mlist.isEmpty();
    }


    /**
     * 添加数据
     */
    public void addItems(List<T> itemsList) {
        mlist.addAll(itemsList);
        notifyDataSetChanged();
    }


    /**
     * 设置新数据,旧数据会清空
     */
    public void setItems(List<T> itemsList) {
        mlist.clear();
        mlist = itemsList;
        notifyDataSetChanged();
    }


    /**
     * 清空数据
     */
    public void cleanItems() {
        mlist.clear();
        notifyDataSetChanged();
    }


    @Override
    public int getCount() {
        return (null == mlist || mlist.size() == 0) ? 0 : mlist.size();
    }


    @Override
    public Object getItem(int position) {
        return position;
    }


    @Override
    public long getItemId(int position) {
        return position;
    }


    @Override
    abstract public View getView(int position, View convertView,
                                 ViewGroup parent);


}
